home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / ov-struct.cc < prev    next >
C/C++ Source or Header  |  1996-11-07  |  3KB  |  134 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include <iostream.h>
  32.  
  33. #include "error.h"
  34. #include "ov-struct.h"
  35. #include "unwind-prot.h"
  36.  
  37. octave_allocator
  38. octave_struct::allocator (sizeof (octave_struct));
  39.  
  40. int
  41. octave_struct::t_id (-1);
  42.  
  43. const string
  44. octave_struct::t_name ("struct");
  45.  
  46. octave_value
  47. octave_struct::struct_elt_val (const string& nm, bool silent) const
  48. {
  49.   octave_value retval;
  50.  
  51.   Pix idx = map.seek (nm);
  52.  
  53.   if (idx)
  54.     retval = map.contents (idx);
  55.   else if (! silent)
  56.     error ("structure has no member `%s'", nm.c_str ());
  57.  
  58.   return retval;
  59. }
  60.  
  61. octave_value&
  62. octave_struct::struct_elt_ref (const string& nm)
  63. {
  64.   return map [nm];
  65. }
  66.  
  67. void
  68. octave_struct::print (ostream& os, bool)
  69. {
  70.   // XXX FIXME XXX -- would be nice to print the output in some
  71.   // standard order.  Maybe all substructures first, maybe
  72.   // alphabetize entries, etc.
  73.  
  74.   begin_unwind_frame ("octave_struct_print");
  75.  
  76.   unwind_protect_int (struct_indent);
  77.   unwind_protect_int (Vstruct_levels_to_print);
  78.  
  79.   if (Vstruct_levels_to_print-- > 0)
  80.     {
  81.       os.form ("\n%*s{\n", struct_indent, "");
  82.  
  83.       increment_struct_indent ();
  84.  
  85.       Pix p = map.first ();
  86.  
  87.       while (p)
  88.     {
  89.       bool pad_after = false;
  90.  
  91.       string key = map.key (p);
  92.       octave_value val = map.contents (p);
  93.  
  94.       map.next (p);
  95.  
  96.       os.form ("%*s%s =", struct_indent, "", key.c_str ());
  97.  
  98.       if (val.print_as_scalar ())
  99.         os << " ";
  100.       else if (val.is_map ())
  101.         {
  102.           if (p)
  103.         pad_after = true;
  104.         }
  105.       else
  106.         {
  107.           if (p)
  108.         pad_after = true;
  109.  
  110.           os << "\n\n";
  111.         }
  112.  
  113.       val.print (os);
  114.  
  115.       if (pad_after)
  116.         os << "\n";
  117.     }
  118.  
  119.       decrement_struct_indent ();
  120.  
  121.       os.form ("%*s%s", struct_indent, "", "}\n");
  122.     }
  123.   else
  124.     os << " <structure>\n";
  125.  
  126.   run_unwind_frame ("octave_struct_print");
  127. }
  128.  
  129. /*
  130. ;;; Local Variables: ***
  131. ;;; mode: C++ ***
  132. ;;; End: ***
  133. */
  134.